package com.ruoyi.web.controller.bi.model;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.ruoyi.bi.domain.model.Dataset;
import com.ruoyi.bi.service.model.DataSourceService;
import com.ruoyi.bi.service.model.DatasetService;
import com.ruoyi.bi.util.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping(value = "/model")
public class DatasetController extends BaseController {

	@Autowired
	private DatasetService datasetService;

	@Autowired
	private DataSourceService dsservice;

	private static Logger logger = Logger.getLogger(DatasetController.class);

	@GetMapping("/listDataset")
	public AjaxResult list(){
		return AjaxResult.success(datasetService.list());
	}

	@PostMapping("/listTables")
	public AjaxResult listTables(@RequestBody Map map) throws Exception{
		return AjaxResult.success(dsservice.listTables((String) map.get("dsid"), (String) map.get("tname")));
	}

	@GetMapping("/listTableColumns")
	public 	AjaxResult listTableColumns(@RequestParam String dsid,@RequestParam String tname) throws Exception {
		return AjaxResult.success(datasetService.listTableColumns(dsid, tname));
	}

	@PostMapping("/queryDatasetMeta")
	public AjaxResult queryDatasetMeta(@RequestBody Map map) throws Exception {
        String cfg = (String) map.get("cfg");
        String dsid = (String) map.get("dsid");
		JSONObject dset = (JSONObject)JSON.parse(cfg);
		return AjaxResult.success(datasetService.queryMetaAndIncome(dset, dsid));
	}

	@GetMapping("/queryDataset")
	public 	AjaxResult queryDataset(@RequestParam String dsetId,@RequestParam String dsid) {
		try {
			return AjaxResult.success(datasetService.queryDsetDatas(dsid, dsetId));
		}catch (Exception ex){
			logger.error("查询出错", ex);
			return AjaxResult.error(ex.getMessage());
		}
	}

	@PutMapping("/updateDset")
	public 	AjaxResult updateDset(@RequestBody Dataset dset)  {
		datasetService.updateDset(dset);
		return AjaxResult.success();
	}

	@PostMapping("/saveDset")
	public 	AjaxResult saveDset(@RequestBody Dataset dset)  {
		datasetService.save(dset);
		return AjaxResult.success();
	}

	@DeleteMapping("/deleteDset/{dsetId}")
	public AjaxResult deleteDset(@PathVariable String dsetId)  {
		datasetService.removeById(dsetId);
		return AjaxResult.success();
	}

	@GetMapping("/getDatasetCfg")
	public AjaxResult getDatasetCfg(@RequestParam String dsetId)  {
		Dataset dataset = datasetService.getById(dsetId);
		JSONObject jsonObject = JSONObject.parseObject(dataset.getCfg());
		jsonObject.put("dsetId", dsetId);
		return AjaxResult.success(jsonObject);
	}

	@RequestMapping("/reloadDset")
	public AjaxResult reloadDset(@RequestParam String dsetId,@RequestParam String dsid)  {
		try{
			datasetService.reloadDset(dsetId, dsid);
			return AjaxResult.success();
		}catch(Exception ex){
			ex.printStackTrace();
			return AjaxResult.error(ex.getMessage());
		}
	}
}
